#include "myitem.h"

MyItem::MyItem(QString img_path, int status)
{
    //图元的图片
    this->img.load(img_path);
    this->status = status;
}

//1.返回图元在场景中的坐标---矩形区域
QRectF MyItem::boundingRect() const
{
    return QRectF(-this->img.width()/2, -this->img.height()/2, this->img.width(), this->img.height());
}

void MyItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
    Q_UNUSED(option);
    Q_UNUSED(widget);
    painter->drawPixmap(-this->img.width()/2, -this->img.height()/2, this->img.width(), this->img.height(), this->img);
}

void MyItem::advance(int)
{
    if(this->status==1)
    {
        //碰撞检测      QList<QGraphicsItem*>  碰撞的图元列表
        if(this->collidingItems().size()>0){
            qDebug() << "boom" ;
            //
            emit stopTimer();
            this->status = 0;       //撞到了状态变为0
            return ;
        }
        this->setPos(mapToScene(4, 0));  //向右移动
    }
    if(this->status==2)
    {
        this->setPos(mapToScene(2, 0));
    }

}


